﻿using System;
using System.Collections.Generic;
using System.Configuration;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;

namespace com.huigo.B2B.Web.center
{
    public partial class Pay : BasePage, ITransfer
    {
        public decimal AlipayTotalPayPrice = 0;
        public decimal PayPrice = 0;
        private PayInfo payInfo = new PayInfo();

        public long PayId
        {
            get
            {
                long payId = 0;
                if (!string.IsNullOrEmpty(Request["pid"]))
                    long.TryParse(Request["pid"], out payId);
                return payId;
            }
        }

        public decimal AlipayRate
        {
            get
            {
                return Convert.ToDecimal(ConfigurationManager.AppSettings["AlipayRate"]);
            }
        }

        private string PayType
        {
            get { return string.IsNullOrEmpty(RequestString("paytype")) ? string.Empty : RequestString("paytype"); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (VerifyPayInfo())
            {
                if (!string.IsNullOrEmpty(PayType))
                {
                    PayIt();
                }
            }
            else
            {
                MessageBox.ShowAndRedirect(this.Page, "支付信息不正确！", "/center/");
            }
        }

        private void SetBalance()
        {
            List<RequestFuncParams> list = new List<RequestFuncParams>();
            list.Add(new RequestFuncParams("custId", CustId.ToString()));
            BaseService service = new BaseService();
            ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_CUST_USEFUL_FEE, list);
            if (parser != null && parser.RowsCount > 0)
            {
                decimal usefulFee = Convert.ToDecimal(parser["CustUsefulFee"]);
                if (usefulFee < PayPrice)
                    litBalance.Text = "您的可用余额：" + usefulFee.ToString("C2");
                else
                    litBalance.Text = "您的可用余额：" + usefulFee.ToString("C2") + "&nbsp;&nbsp;<input name=\"paytype\" value=\"BALANCE\" type=\"hidden\" /><input type=\"submit\" id=\"balanceSubmit\" name=\"button\" class=\"button btn-big btn-orange\" value=\"余额支付\" />";
            }
        }

        private void PayIt()
        {
            switch (PayType.ToLower())
            {
                case "balance":
                    payInfo.PayType = Dict.PayType.BALANCE.ToString();
                    payInfo.TotalFee = PayPrice;
                    TransPayInfo = payInfo;
                    Server.Transfer("~/pay/PayResult.aspx");
                    break;
                case "alipay":
                    payInfo.PayType = Dict.PayType.ALIPAY.ToString();
                    payInfo.TotalFee = AlipayTotalPayPrice;
                    TransPayInfo = payInfo;
                    Server.Transfer("~/pay/alipay/Redirect.aspx");
                    break;
            }
        }

        private bool VerifyPayInfo()
        {
            bool isVerified = false;
            SetFeeInfo();
            SetBalance();
            if (PayPrice > 0)
            {
                isVerified = true;
            }
            return isVerified;
        }

        private void SetFeeInfo()
        {
            if (PayId != 0)
            {
                switch (PayId % 1000)
                {
                    case 6://订单
                        List<RequestFuncParams> list = new List<RequestFuncParams>();
                        list.Add(new RequestFuncParams("orderId", PayId.ToString()));
                        list.Add(new RequestFuncParams("custId", CustId.ToString()));
                        BaseService service = new BaseService();
                        ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_UNPAY_ORDER_INFO, list);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            PayPrice = Convert.ToDecimal(parser["OrderPayAmount", 0]);
                            AlipayTotalPayPrice = ConvertCurrencyPay(PayPrice / (1 - AlipayRate));
                            payInfo.PayNo = parser["OrderNo", 0];
                            payInfo.Fee = PayPrice;
                            payInfo.PayId = PayId;
                            payInfo.CustId = CustId;
                            payInfo.PayFor = (int)Dict.PayFor.ORDER;
                        }
                        break;
                    case 7://发票申领
                        List<RequestFuncParams> list7 = new List<RequestFuncParams>();
                        list7.Add(new RequestFuncParams("orderId", PayId.ToString()));
                        list7.Add(new RequestFuncParams("custId", CustId.ToString()));
                        BaseService service7 = new BaseService();
                        ParserXml parser7 = service7.RequestEntryService(Dict.ERequestFunc.GET_UNPAY_INVOICE_ORDER_INFO, list7);
                        if (parser7 != null && parser7.RowsCount > 0)
                        {
                            PayPrice = Convert.ToDecimal(parser7["OrderPayAmount", 0]);
                            AlipayTotalPayPrice = ConvertCurrencyPay(PayPrice / (1 - AlipayRate));
                            payInfo.PayNo = parser7["OrderNo", 0];
                            payInfo.Fee = PayPrice;
                            payInfo.PayId = PayId;
                            payInfo.CustId = CustId;
                            payInfo.PayFor = (int)Dict.PayFor.INVOICE;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        #region ITransfer Members

        public PayInfo TransPayInfo
        {
            get;
            set;
        }

        #endregion
    }
}
